﻿

IncludePath "Source/"
IncludeFile "threeD.pb"
IncludePath "/"

EnableExplicit

#Program_WindowNum = 0

;{ Structures
	Structure vertex
		pos.vec3
	EndStructure
;}
;{ Globals
	;Global VertexsAmount.l
	Global shaderID.l
	Global TextureID.l
	Global hDC.l
	Global MatProj.mat4
	Global MatModelView.mat4
	Global *mesh.AnimatedMesh
	
	Global frameNumber.f = 0
;}
;{ Procedures

	Procedure UpdateModelMat()
		Protected.vec4 pos, Q, Q1, Q2
		Protected.mat3 MatRot3
		Protected.mat4 MatTrans, MatRot4
		Protected.f angleV, angleH
		
		angleV = WindowMouseY(#Program_WindowNum)/20.0
		angleH = WindowMouseX(#Program_WindowNum)/20.0
		
		;Set model center
		Mat4_CreateTranslationComp(MatTrans, -(*mesh\BBoxMax\x + *mesh\BBoxMin\x)/2.0, -(*mesh\BBoxMax\y + *mesh\BBoxMin\y)/2.0, -(*mesh\BBoxMax\z + *mesh\BBoxMin\z)/2.0)
		
		Quat_FromAxisComp(Q1, 1,0,0, angleV)
		Quat_FromAxisComp(Q2, 0,0,1, angleH)
		Quat_MulQ(Q, Q1, Q2)
		Mat3_FromQuat(MatRot3, Q)
		
		Vec4_Set(pos, 0, 0, -50, 1)
		Mat4_SetM(MatRot4, MatRot3, pos)
		
		Mat4_MulM(MatModelView, MatRot4, MatTrans)
	EndProcedure
	
	Procedure Init()
		UsePNGImageDecoder()
		OpenWindow(#Program_WindowNum, 100, 200, 512, 512, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
		
		hDC = CreateOpenGLContext(WindowID(0))		
		ShaderID = LoadShadersFromFiles("Data/md3shader.vs", "Data/md3shader.fs", "Data/md3shader.gs")
		TextureID = OpenGL_ImageToTexture(LoadImage(#PB_Any, "Data/skin.png"))
		
		Protected mdl.md3entity
		LoadMD3(@mdl, "Data/gman.md3")
		*mesh = CreateAnimatedMeshFromMD3(@mdl)
		
		UpdateModelMat()
		
		
		glEnable_(#GL_DEPTH_TEST)
	EndProcedure
	
	Procedure RenderScene()
	  Protected.l uniformLocation
	  
		glUseProgram(shaderID)
		
		;Юниформы
		uniformLocation = glGetUniformLocation(shaderID, @"u_ProjectionMat")
		If uniformLocation <> -1
			glUniformMatrix4fv(uniformLocation, 1, #True, @MatProj)
		EndIf
		uniformLocation = glGetUniformLocation(shaderID, @"u_ModelViewMat")
		If uniformLocation <> -1
			glUniformMatrix4fv(uniformLocation, 1, #True, @MatModelView)
		EndIf	

	;делаем активным текстурный юнит 0
	glActiveTexture(#GL_TEXTURE0);
	;назначаем текстуру на активный текстурный юнит
	glBindTexture(#GL_TEXTURE_2D, TextureID);
	
		uniformLocation = glGetUniformLocation(shaderID, @"u_BaseTexture")
		If uniformLocation <> -1
			glUniform1i(uniformLocation, 0)
		EndIf
		
		
		Protected blend.f = frameNumber-Int(frameNumber)


		uniformLocation = glGetUniformLocation(shaderID, @"u_Blend")
		If uniformLocation <> -1
			glUniform1f(uniformLocation, blend)
		EndIf
		
		DrawAnimatedMesh(*mesh, Int(frameNumber), Int(frameNumber)+1, blend)
	EndProcedure
	
	
	Procedure RunStart()
		Protected.l Event, Quit
		
		Repeat
			Event = WaitWindowEvent(1)
			Select Event
				Case #PB_Event_CloseWindow	
					Quit = 1
				Case #PB_Event_SizeWindow
					glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
					Mat4_CreatePerspective(MatProj, #PI/2, WindowWidth(0)/WindowHeight(0), 0.1, 1000.0)
			EndSelect
			
			
			frameNumber = frameNumber + 0.01
			If (frameNumber >= *mesh\NumOfFrames)
				frameNumber = 0
			EndIf	
			
			glClearColor_(0.3,0.3,0.3,1)
			glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
			UpdateModelMat()
			RenderScene()
			
			SwapBuffers_(hDC)
		Until Quit = 1
	EndProcedure
;}



Init()
RunStart()
; IDE Options = PureBasic 5.11 (Windows - x86)
; CursorPosition = 37
; FirstLine = 7
; Folding = --
; EnableXP